import collections


class Solution(object):
    def rankTeams(self, votes):
        m = len(votes[0])
        d = collections.defaultdict(lambda: [0] * m)
        for vote in votes:
            for index, c in enumerate(vote):
                d[c][index] -= 1
        out = [[d[key], key] for key in d]
        out.sort()
        s = [p[1] for p in out]
        return ''.join(s)

data = Solution()
votes = ["ABC", "ACB", "ABC", "ACB", "ACB"]
print(data.rankTeams(votes))
